<?php
include ('items/vardef.php');

global $aCCss;
global $aCJs;
$aCCss = array();
$aCJs = array();

/////////////////////////////// HELPER ///////////////////////////////
//get assest : image, css, js
function assest($path)
{
    return get_template_directory_uri() . '/' . $path;
}
function load_view($file, $params)
{
    ob_start();
    extract($params);
    require ($file);
    $content = ob_get_clean();
    return $content;
}
function register_css($params)
{
    global $aCCss;
    if(is_array($params)){
        $aCCss = $params;
    } else {
        $aCCss[] = $params;
    }
}
function register_js($params)
{
    global $aCJs;
    if(is_array($params)){
        $aCJs = $params;
    } else {
        $aCJs[] = $params;
    }
}
function setMessage($cmd, &$aError, $key, $aType){
    $aError[] = array('key'=>$cmd['vardef']->trans($key), 'etext'=> $cmd['theValidate']->trans($aType));
}
/////////////////////////////// END HELPER ///////////////////////////////


/////////////////////////////// AUTO SETTING ///////////////////////////////
add_theme_support('post-thumbnails');
// update_option('contact_facebook', 'pjimex');
// update_option('contact_twitter', 'pjimex');
// update_option('contact_googleplus', 'pjimex');
// update_option('contact_rss', 'pjimex');
// update_option('contact_skype', 'hoangpj');
// update_option('contact_mail', 'dntruong412@gmail.com');
// update_option('name', 'Dinh Nhat Truong');
// update_option('address', 'truongdn.testmail@gmail.com');
// update_option('mluname', 'truongdn.testmail');
// update_option('mlupass', '#TruongDN@pwd-testMail');
/////////////////////////////// END AUTO SETTING ///////////////////////////////


/////////////////////////////// FILTER ///////////////////////////////
//register a top nav
function register_my_menu()
{
    register_nav_menu('header-menu', __('Header Menu'));
}
add_action('init', 'register_my_menu');

function new_excerpt_more( $more ) {
    return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

function custom_excerpt_length( $length ) {
	return 40;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
/////////////////////////////// END FILTER ///////////////////////////////

/////////////////////////////// AJAX ///////////////////////////////
add_action('phpmailer_init', 'my_phpmailer_example');
function my_phpmailer_example($phpmailer)
{
    $phpmailer->IsSMTP();
    $phpmailer->SMTPAuth = true;
    $phpmailer->SMTPSecure = "ssl";
    $phpmailer->Host = 'smtp.gmail.com';
    $phpmailer->Port = 465;
    $phpmailer->Username = get_option('mluname');
    $phpmailer->Password = get_option('mlupass');
    $phpmailer->SetFrom(get_option('address'), get_bloginfo('name') . " - Contact mail");
    $phpmailer->isHTML(true);
}

add_action('wp_ajax_site_contact', 'site_contact');
add_action('wp_ajax_nopriv_site_contact', 'site_contact');
function site_contact()
{
    $aError = array();
    $aResult = array('code'=>0, 'msg'=>"Send contact fail !");
    $aContact = $_POST['contact'];
    $vardef = new VarDef();
    $theValidate = new TheValidate();
    $cmd = array('vardef'=>$vardef, 'theValidate'=>$theValidate);
    foreach ($aContact as $key => &$c) {
        //check length
        $c = trim($c);
        if($c==''){
            setMessage($cmd, $aError, $key, 'blank');
        } else {
            //check name
            if($key=='name'){
                if(strlen($c)<8 || strlen($c)>30){
                    setMessage($cmd, $aError, $key, array('ValidLength'=>array('min'=>8, 'max'=>30)));
                }
            }
            //check email
            if($key=='email'){
                if(!filter_var($c, FILTER_VALIDATE_EMAIL)){
                    setMessage($cmd, $aError, $key, 'email');
                }
            }
            //check subject
            if($key=='subject'){
                if(strlen($c)<8 || strlen($c)>100){
                    setMessage($cmd, $aError, $key, array('ValidLength'=>array('min'=>8, 'max'=>100)));
                }
            }
            //check comment
            if($key=='content'){
                if(strlen($c)<8 || strlen($c)>1500){
                    setMessage($cmd, $aError, $key, array('ValidLength'=>array('min'=>8, 'max'=>1500)));
                }
            }
        }
    }
    if(count($aError)==0){
        if(wp_mail(get_option('contact_mail'), $aContact['subject'], load_view('items/mail-contact.html.php', $aContact))){
            $aResult = array('code'=>1, 'msg'=>"Send contact successfully !");
        }
    } else {
        $aResult['details'] = $aError;
    }
    wp_send_json($aResult);
    die();
}
/////////////////////////////// END AJAX ///////////////////////////////
?>